package org.wen.soft.generate.util;

import org.wen.soft.generate.model.GeneClass;

import java.util.HashMap;
import java.util.Map;

public class MappingUtil {

    public static final Map<String, String> basicTypeMappings = new HashMap<String, String>(){{
        put("byte", "Byte.parseByte(\"1\")");
        put("char", "'2'");
        put("boolean", "true");
        put("short", "Short.parseShort(\"1\")");
        put("int", "20201212");
        put("long", "20201212");
        put("float", "20201212F");
        put("double", "20201212D");
    }};

    public static final Map<String, String> basicArrayTypeMappings = new HashMap<String, String>(){{
        put("byte[]", "new byte[]{1, 0}");
        put("char[]", "new char[]{1, 0}");
        put("boolean[]", "new boolean[]{true, false}");
        put("short[]", "new short[]{1, 0}");
        put("int[]", "new int[]{20221212, 20221212}");
        put("long[]", "new long[]{20221212, 20221212}");
        put("float[]", "new float[]{20221212F, 20221212F}");
        put("double[]", "new double[]{20221212D, 20221212D}");
    }};

    public static final Map<String, String> basicObjectTypeMappings = new HashMap<String, String>(){{
        put("java.lang.Byte", "Byte.valueOf(\"1\")");
        put("java.lang.Character", "'1'");
        put("java.lang.Boolean", "true");
        put("java.lang.Short", "Short.valueOf(\"1\")");
        put("java.lang.Integer", "20221212");
        put("java.lang.Long", "20221212L");
        put("java.lang.Float", "20221212F");
        put("java.lang.Double", "20221212D");
    }};

    public static final Map<String, String> basicObjectArrayTypeMappings = new HashMap<String, String>(){{
        put("java.lang.Byte[]", "new Byte[]{1, 0}");
        put("java.lang.Character[]", "new Character[]{1, 0}");
        put("java.lang.Boolean[]", "new Boolean[]{true, false}");
        put("java.lang.Short[]", "new Short[]{1, 0}");
        put("java.lang.Integer[]", "new Integer[]{20221212, 20221212}");
        put("java.lang.Long[]", "new Long[]{20221212L, 20221212L}");
        put("java.lang.Float[]", "new Float[]{20221212F, 20221212F}");
        put("java.lang.Double[]", "ew Double[]{20221212D, 20221212D}");
    }};

    public static final Map<String, String> otherObjectMappings = new HashMap<String, String>(){{
        put("java.lang.String", "\"20221212\"");
        put("java.lang.Object", "\"20221212\"");
        put("java.lang.String[]", "new String[]{\"20221212\", \"20221212\"}");
        put("java.lang.Object[]", "new Object[]{\"20221212\", \"20221212\"}");

        put("java.util.List", "new ArrayList<>()");
        put("java.util.Map", "new HashMap<>()");
        put("java.util.ArrayList", "new ArrayList<>()");
        put("java.util.HashMap", "new HashMap<>()");
        put("java.util.Set", "new HashSet<>()");


        put("java.util.Date", "new Date()");
        put("java.sql.Timestamp", "new Timestamp(1)");
        put("java.math.BigDecimal", "new BigDecimal(\"1\")");
    }};


    public static final Map<String, String> mockMappings = new HashMap<String, String>(){{
        put("byte", "anyByte()");
        put("char", "anyChar()");
        put("boolean", "anyBoolean()");
        put("short", "anyShort()");
        put("int", "anyInt()");
        put("long", "anyLong()");
        put("float", "anyFloat()");
        put("double", "anyDouble()");
        put("Object", "any()");
    }};

    /**
     * 根据类型获得值： 通常作为方法参数值模拟
     */
    public static String mappingValue(String classTypeName) {
        if (basicTypeMappings.containsKey(classTypeName)) {
            return basicTypeMappings.get(classTypeName);
        }
        if (basicArrayTypeMappings.containsKey(classTypeName)) {
            return basicArrayTypeMappings.get(classTypeName);
        }
        if (basicObjectTypeMappings.containsKey(classTypeName)) {
            return basicObjectTypeMappings.get(classTypeName);
        }
        if (basicObjectArrayTypeMappings.containsKey(classTypeName)) {
            return basicObjectArrayTypeMappings.get(classTypeName);
        }
        if (otherObjectMappings.containsKey(classTypeName)) {
            return otherObjectMappings.get(classTypeName);
        }
        return "null";
    }

    public static String mappingCanVar(String classTypeName, GeneClass geneClass) {
        // todo 根据类名、字段名匹配变量
        return mappingValue(classTypeName);
    }


    public static String mappingMockValue(String classTypeName){
        if (mockMappings.containsKey(classTypeName)) {
            return mockMappings.get(classTypeName);
        }
        String simpleName = GStrUtil.simpleName(classTypeName);
        return "any(" + simpleName + ".class)";
    }


}
